<div id="wrapMain">
            <div class="wrap_1024" style="margin-bottom:40px">

            <div class='head_nav'><span class="icon-picture"></span> Get story</div>
            <div class="user_profile">
				
				<div class="row" >
					<div class="col-md-12 ">
						<div id="contentIndex" class="col-md-12">
										<div role="dialog" id="modalAlertBox" class="modal fade" style="display: none;">
											<div class="modal-dialog modal-wide">
												<div class="modal-content">
													<div class="modal-header">
														<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
														<h4 class="modal-title"></h4>
													</div>
											        <div class="modal-body">
														
														<div style="clear:both"></div>
											        </div>
												</div><!-- /.modal-content -->
											</div>
										</div>
										
										<div id="wrapLoadMoreItems">
											<div class="normalLoadBtn" id="loadMoreItems"><img width="20" alt="loading" src="public/images/loading.gif"> Loading </div><br>
										</div>
										<div class="formLoading"></div>
										
									    <div class="generalError" style="display: none;"></div>
									    <form id="addStory" class="userPostForm" role="form" method="post" enctype="multipart/form-data">
											
											<div class="form-group">
												<div class="row">
                                                <div class="col-md-2">
												<label for="txtTitle">Title</label>
                                                </div>
                                                <div class="col-md-10">
												<input type="text" class="form-control" id="txtTitle" placeholder="Story title">
                                                </div>
                                                </div>
											</div>
											
											<div class="form-group">
												<div class="row">
                                                <div class="col-md-2">
												<label for="txtAuthor">Author</label>
                                                </div>
                                                <div class="col-md-10">
												<input type="text" class="form-control" id="txtAuthor" placeholder="Story author">
                                                </div>
                                                </div>
											</div>
											
											<div class="form-group">
												<div class="row">
                                                <div class="col-md-2">
												<label for="txtChaptNums">Numbers of chapter</label>
                                                </div>
                                                <div class="col-md-2">
												<input type="text" class="form-control" id="txtChaptNums" placeholder="Numbers of chapter">
                                                </div>
                                                </div>
											</div>
											
											<div class="form-group">
												<div class="row">
                                                <div class="col-md-2">
												<label for="txtDescription">Short description</label>
                                                </div>
                                                <div class="col-md-10">
												<textarea class="form-control" placeholder="Story description..." id="txtDescription" rows="5"></textarea>
												</div>
                                                </div>
                                            </div>
                                            
                                            <div class="form-group">
												<div class="row">
                                                <div class="col-md-2">
												<label for="txtSource">Nguồn</label>
                                                </div>
                                                <div class="col-md-2">
													<select id="txtSource">
														
														<option value="1">struyen</option>	
														<option value="2">webtruyen</option>	
														<option value="3">tunghoanh</option>	
														
													</select>
												</div>
												<div class="col-md-8">
													<input id="txtSourceUrl" type="" name="" class="form-control"  placeholder="Story Url..." />
												</div>
                                                </div>
                                            </div>
												
											<?php if($data['categories']):?>
											<div class="form-group">
                                            	<div class="row">
                                                <div class="col-md-2">
												<label for="txtCat">Chọn danh mục</label>
                                                </div>
                                                <div class="col-md-10">
												<select id="txtCat" class="myNormalSelect select2">
													<?php foreach($data['categories'] as $value): ?>
													<option value="<?= $value['id']; ?>"><?= $value['name'] ?></option>	
													<?php endforeach; ?>
												</select>
                                                </div>
                                                </div>
											</div>
											<?php endif;?>
											
											<div class="form-group">
                                            	<div class="row">
                                                <div class="col-md-2">
												<label for="txtAlbum">Chọn Album</label>
                                                </div>
                                                <div class="col-md-10">
												<select id="txtAlbum" class="myNormalSelect select2">
                                                <option value="">Tùy Chọn Album</option>	
													<?php if($data['albums']):?>
													<?php foreach($data['albums'] as $value): ?>
													<option value="<?= $value['id']; ?>"><?= $value['name'] ?></option>	
													<?php endforeach; ?>
													<?php endif;?>
												</select>
												
												<button type="submit" class="btn btn-sm createAlbum">
							                    	<i class="glyphicon glyphicon-plus"></i>
								                    <span>Tạo Album mới</span>
								                </button>
                                                </div>
                                                </div>
											</div>
											<br />
                                            
                                       		<div class="form-group">
												<div class="row">
                                                <div class="col-md-2">
												<label for="txtAutoScan">Scan tự động</label>
                                                </div>
                                                <div class="col-md-2">
	                                                <input type="checkbox" name="txtAutoScan" id="txtAutoScan" value="1" />
	                                                
                                                </div>
                                                </div>
											</div>
                                          
                                            <div class="row"> <!-- Start class row wrap upload image and submit button -->
                                            <div class="col-md-2">
                                            </div>
                                            <div class="col-md-10">
											<button type=submit class="btn btn-primary mySubmit" id="submitStory">
							                    <i class="glyphicon glyphicon-hand-up"></i>
							                    <span>Đăng bài</span>
							                </button>
                                            </div> <!-- End of class col-md-10 wrap upload image and submit button -->
                                            </div><!-- End of class row wrap upload image and submit button -->
											
										</form>
				
						</div><!-- end #contentIndex -->
					</div>
					
					
				</div> <!-- End of row -->
				<div class="row" id="report_chapter">
				<table class="story_table" id="report_chapter_table">
					<thead>
						<tr>
							<th align="center">Stt</th>
							<th align="center">Link</th>
							<th align="center">Title</th>
							<th align="center">Page</th>
							<th align="center">Tập</th>
							<th align="center">Chương</th>
							<th align="center">Status</th>
						</tr>
					</thead>
					
					
				</table>
				</div>
			</div><!-- end wrap_1024 -->
		</div><!-- end wrapMain -->
		
<!-- end #wrapMain -->		
		

<script type="text/javascript">
	$(document).ready(function(){
		
		
	    $('.createAlbum').click(function(e){
		    e.preventDefault();
		    $('#txtAlbum_message').remove();
		    $('#modalAlbumBox').remove();
		    var that = this;
			$.ajax({
				url : "<?php echo BASE_URL ?>index.php?module=user&act=ajax&albumBox=1",
				success : function(data){
					$('.createAlbum').parent().append(data);
					$('.createAlbum').parent().find('#modalAlbumBox').modal();
				},
			});
		});

	    $('#txtTitle').blur(function(){
	    	$.ajax({
	    		url: "index.php?module=user&act=ajax&validateTitle=1",
	    		type: 'POST',
	    		data: {txtTitle: $('#txtTitle').val()},
	    		success: function(data){
	    			newData = $.parseJSON(data);
	    			if(newData.errors){
		    			
		    			if($('#txtTitle_error').length <= 0){
			    		
		    				$('#txtTitle').parent().append('<div id="txtTitle_error" class="alert alert-danger alert-dismissable" style="display: none;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'+newData.errors.txtTitle+'</div>');
		    				$('#txtTitle_error').slideDown('fast');
		    			}else{
							$('#txtTitle_error').html(newData.errors.txtTitle);
		    			}
	    				
	    			}else{
	    				$('#txtTitle_error').remove();
	    			}
	    		}
	    	});
	    });

	    $('#txtDescription').blur(function(){
	    	//$(this).parent().find('.alert').remove();
	    	$.ajax({
	    		url: "index.php?module=user&act=ajax&validateDescription=1",
	    		type: 'POST',
	    		data: {txtDescription: $('#txtDescription').val()},
	    		success: function(data){
	    			newData = $.parseJSON(data);
	    			console.log(newData);
	    			if(newData.errors.txtDescription){
	    				if($('#txtDescription_error').length <= 0){
		    				$('#txtDescription').parent().append('<div id="txtDescription_error" class="alert alert-danger alert-dismissable" style="display: none;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'+newData.errors.txtDescription+'</div>');
		    				$('#txtDescription_error').slideDown('fast');
	    				}else{
							$('#txtDescription_error').html(newData.errors.txtDescription);
		    			}
	    			}else{
	    				$('#txtDescription_error').remove();
	    			}
	    		}
	    	});
	    });

	    
	    /*
	     * Submit add Story
	     */
	    $('button#submitStory').click(function(e){
	    	e.preventDefault();
	    	form = $('#addStory');
	    	
	    	$(".alert").remove();
	  		$('.userPostForm').css('opacity', '0.3');
			$('.formLoading')
			.css('width', $('#videoUpload').width())
			.css('height', $('#videoUpload').height())
			.css('opacity', '1')
			.css('z-index','9999').css('display', 'block');
			$('#wrapLoadMoreItems').css('display', 'block').center();

	    	$.ajax({
	    		url : "<?php echo BASE_URL; ?>index.php?module=user&act=ajax&validateGetStory=1",
	    		type : 'POST',
	    		
	    		data : {
		    		txtTitle: $('#txtTitle').val(),
	    			txtAuthor: $('#txtAuthor').val(),
	    			txtDescription : $('#txtDescription').val(),
	    			txtChaptNums : $('#txtChaptNums').val(),
	    			txtCat : $('#txtCat').val(),
	    			txtAlbum : $('#txtAlbum').val(),
	    			txtSource: $('#txtSource').val(),
	    			txtSourceUrl: $('#txtSourceUrl').val(),
	    			txtAutoScan : $('#txtAutoScan').prop('checked') ? 1 : 0,
	    		},
	    		
	    		success : function(data){
		    		if(data) newData = $.parseJSON(data);
		    		//Trường hợp thành công
		    		if(newData.message){
		    			popupMessage('Thông báo', [newData.message], 'alert-info');
		    		//Trường hợp thất bại
		    		}else if(newData.errors){
			    		allErrors = '';
						$.each(newData.errors, function(ee, iindex){
							allErrors += iindex + '<br />';
						});
						popupMessage('Thông báo', [allErrors], 'alert-danger');
		    		}
		    		$('.formLoading').hide();
		    		$('#wrapLoadMoreItems').hide();
		    		$('.userPostForm').css('opacity', '1');
		    	}
	    	});
	    	
	    });

	   	function getChaptersViaPage(newData){
			
		   	startPage = newData.startPage;
		   	
	   		newurl = newData.url.replace('%s%', startPage);
	  
	   		returnSend = false;
	   		
	   		$.ajax({
				url: "<?php echo HOME; ?>index.php?module=user&act=ajax&validateGetChapter=1",
				type: 'POST',
				cache: false,
				//dataType: 'jsonp',
				async: false,
				timeout: 800000,
				data: {txtSource: $('#txtSource').val(), txtSourceUrl: newurl},
				success: function(data1){
					console.log(data1);
					if(data1 != 'het'){
						newData1 = $.parseJSON(data1);
						
							$.ajax({
								url: "<?php echo HOME; ?>index.php?module=user&act=ajax&insertChapter=1",
								type: 'POST',

								data: {links: data1, storyData: newData},
								success: function(insertData){
									
									i = 1;
									$.each(newData1, function(e,index){
										if(i%2==0){trclass = 'class="odd"';}else{trclass='class="even"';}
										if(index.href == 'Error') {status = '<td class="fail_story">Fail</td>';}else{status = '<td>OK</td>'}
										$('#report_chapter_table').append('<tr '+trclass+'><td>'+i+'</td><td>'+index.link+'</td><td>' + index.title + '</td><td>'+newData.startPage+'</td><td>'+index.tap+'</td><td>'+index.chapt+'</td>'+status);
										i++;
									});
								},
							});
						
						//console.log('here if khac false - ' + data1);
						//if(data1) returnData = $.parseJSON(data1);
						
					}else{
						//console.log('here returnSend true');
						console.log('het');
						returnSend = true;
						return false;
					}
					
				},

				error: function(x, t, m) {
					newData.startPage = newData.startPage - 1;
			        if(t==="timeout") {
			            alert("got timeout");
			        } else {
				        console.log(x);
				        console.log(t);
				        console.log(m);
			        }
			    },
				
				complete: function(jqXHR, textStatus){
					
					if(returnSend == true){
						console.log('returnSend ' + returnSend);
						return;
					}

					newData.startPage = newData.startPage + 1;
					getChaptersViaPage(newData);
				}
			});
	   	}
	    
	    $('.select2').select2({ width: '250' });
		$('#txtSource').select2({width: '150'});
	});
</script>

<script src="public/exts/js/jquery.center.js"></script>

<?php 
echo '<pre>';
print_r($_SESSION['chapts']);
echo '</pre>';
?>
